package cloudspace.util;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.DailyRollingFileAppender;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.PropertyConfigurator;


public class CloudLogInit extends HttpServlet
{
    /**
	 * 
	 */
    private static final long serialVersionUID = 8196006765173485893L;


    @Override
    public void init()
    {
        String prefix = getServletContext().getRealPath( "/" );
        String file = getInitParameter( "log4j-init-file" );
        // if the log4j-init-file is not set, then no point in trying
        if ( file != null )
        {
            PropertyConfigurator.configure( prefix + file );
        }

        File prefixFile = new File( prefix );
        String tomcatDirectory = prefix + "logs" + File.separatorChar;
        String logFile = tomcatDirectory + prefixFile.getName() + "-error"
            + ".log";
        DailyRollingFileAppender append = null;
        try
        {
            append = new DailyRollingFileAppender( new PatternLayout( "%d{yyyyMMdd HH:mm:ss,SSS} %5p [%t] (%F:%L) - %m%n" ),
                logFile,
                "'.'yyyy-MM-dd" );
        }
        catch ( IOException e )
        {
            e.printStackTrace();
        }
        Logger.getRootLogger().addAppender( append );
    }


    @Override
    public void doGet( HttpServletRequest req, HttpServletResponse res )
    {
    }

}
